import formJs from "./regexDetail.js"; //新增弹框配置项
import publicForm from "@/components/public/el-forms.vue"; //弹框form
import CustomDialog from "@/components/HntManage/pouringOrder/WBSHuitangangbao/index.vue";
import CustomDialog1 from "@/components/reboundStrengthManage/reboundEntrustList/index.vue";//委托单列表页
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";
import API from '@/utils/commonRequest.js'

import {
    getConcreteCheckTaskByFid, //详情
    saveConcreteCheckTask, //保存
    getCheckitemsList, //试验检测依据-获取试验依据，检测依据
    getMangEquipmentPageList, //主要仪器设备名称及编号
    getBaseDataitemDetailByItemCode, //设计强度等级(Qdlevel)
    // getCheckTypeByUserList,//韩磊商量封掉暂时无用--cnn
    getUserCompanyTreeList
} from "@/api/request.js";
export default {

    data() {
        return {
            visible: "",
            // WBS
            WBSShow: false,
            // WBS
            // 委托单列表页
            isShow: false,
            // 委托单列表页

            loading: false,
            title: "",
            isLock: false, //保存按钮是否可编辑
            dialogHeight: null,
            angleData: [
                //   {
                //   value: '-1',
                //   label: '0°'
                // },
                {
                    value: "0",
                    label: "90°",
                },
                {
                    value: "1",
                    label: "60°",
                },
                {
                    value: "2",
                    label: "45°",
                },
                {
                    value: "3",
                    label: "30°",
                },
                {
                    value: "4",
                    label: "-30°",
                },
                {
                    value: "5",
                    label: "-45°",
                },
                {
                    value: "6",
                    label: "-60°",
                },
                {
                    value: "7",
                    label: "-90°",
                },
            ], //角度数据
            formData: {
                fId: "", //传fid表示修改 不传表示新增
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fLeadercompanyfullname: "",
                fLeadercompanyname: "",
                fCompanyid: "", //施工单位id 隐藏
                fCompanyname: "", //施工单位名称 隐藏
                fProjectid: "", //项目id 隐藏
                fProjectname: "", //项目名称 隐藏   工程名称
                // fCompanyfullname: "", //工程名称
                fProjecttypeid: "", //工程部位id
                fProjecttypename: "", //工程部位
                fPlandates: this.common.day(0), //计划检测日期
                fTesttype: "", //试验条件
                fTestnote: "", //检测依据
                fJudgetype: "", //判定依据
                fEquipmentname: "", //主要仪器设备及编号*
                fEquipmentid: "", //主要仪器设备及编号id*
                fTestnote1: [], //检测依据
                fJudgetype1: [], //判定依据
                fEquipmentname1: [], //主要仪器设备及编号*
                fSpecimenage: "", //龄期
                fConcretelevel: "", //设计强度等级(MPa)*
                fConcretetype: "", //是否泵送
                fDirection: "", //回弹测试方向*
                fSurface: "", //测面状态
                fAngle: "", //测试角度
                fCreateuserid: "", //编制人id
                fCreateusername: "", //编制人
                fNature: "",//当前用户机构性质

                // isSubmit: "0", //0 保存 1提交
            },
            formObj: formJs.formObj, //报验信息

            btnShow: true,
            currentDetail: null,
            timeYMD: null, //当前日期
            timeYmdHms: null,
            selected: [], //多选接值
            primaryInfo: null,
            userNature: null,
            getList: null,

        };
    },
    components: {
        publicForm,
        CustomDialog,
        CustomDialog1,
        FormLyout,
    },


    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))

        console.log(8585,this.primaryInfo);
        this.statusNum = this.primaryInfo.status;
        this.getList = this.primaryInfo.getList;
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser
            // id = data[0].projectId;
        }, true);
        this.getBaseDataitemDetailByItemCode('Qdlevel') //设计强度等级(Qdlevel)
        this.getCheckitemsList("3"); //回弹试验依据
        this.getCheckitemsList("4"); //回弹判定依据
        this.getEquipment() //主要仪器设备名称及编号
        API.userNature().then((res) => {
            if (res.errcode == 0) {
                this.userNature = res.data.Nature;
                this.formData.fNature = res.data.Nature;
                this.formData.fCompanyid = this.primaryInfo.fCompanyid;
                this.btnShow = true;
                this.formObj.map(item => {
                    item.disabled = false;
                });
                this.formObj[0].disabled = true;
                if (this.statusNum == 1) {
                    if (this.userNature == "施工单位") {
                        this.formObj[1].disabled = true;
                        this.getTree(0, this.primaryInfo.fCompanyid)
                    } else if (this.userNature == "总监办") {
                        this.getTree(1, this.getList.fprojectid)
                        this.formObj[1].disabled = false;
                    } else if (this.userNature == "中心试验室") {
                        this.getTender(this.getList.fprojectid);
                        this.formObj[0].disabled = false;
                        this.formObj[1].disabled = false;
                    }

                    this.formObj[14].disabled = true;
                    this.formData.fCreateuserid = this.userInfo.id; // //编制人id
                    this.formData.fCreateusername = this.userInfo.userName; // //编制人
                    this.formData.fProjectid = this.userInfo.projectId; // 当前用户项目ID
                    this.formData.fProjectname = this.userInfo.projectName; // 当前用户项目名称
                    this.formObj[2].inputButton = true;
                } else if (this.statusNum == 3) {
                    if (this.userNature == "施工单位") {
                        this.getTree(0, this.primaryInfo.fCompanyid)
                        this.formObj[1].disabled = true;
                    } else if (this.userNature == "总监办") {
                        this.getTree(1, this.getList.fprojectid)
                        this.formObj[1].disabled = false;
                    } else if (this.userNature == "中心试验室") {
                        this.getTender(this.getList.fprojectid);
                        this.formObj[0].disabled = false;
                        this.formObj[1].disabled = false;
                    }
                    this.getDetail(this.primaryInfo.data)
                    this.formObj[14].disabled = true;

                    this.formObj[2].inputButton = true;


                } else if (this.statusNum == 7) {


                    this.getDetail(this.primaryInfo.data)
                    this.btnShow = false;
                    this.formObj.map(item => {
                        item.disabled = true;
                    })
                    this.formObj[2].inputButton = false;

                    //    this.getDetail({
                    //     fId: this.currentDetail.fId
                    // })
                }

            }
        }).catch((err) => {
            console.log("API", err)
        })




    },
    methods: {
        refreshList() { },
        WBSDialog() { // WBS树关闭
            this.WBSShow = false;
        },
        // WBS树展示
        clickDialog(index, prop) {
            // console.log("clickDialog", index, prop);
            if (prop == "fProjecttypename") {
                this.WBSShow = true;
            }
            return index, prop;
        },
        getTree(num, id) {
            var data = {
                companyId: id,
                userId: this.userInfo.id
            }
            this.formObj[1].options = [];
            getUserCompanyTreeList(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        if (num == 0) {
                            this.formData.fCompanyname = companyTree[0].fShortname;
                            this.formData.fCompanyid = companyTree[0].fCompanyid;
                        } else if (num == 1) {
                            var info = this.commonJs.tranListToTreeData(companyTree)[0].children;
                            info.map(item => {
                                this.formObj[1].options.push({
                                    label: item.fShortname,
                                    value: item.fCompanyid
                                })
                            })
                            this.formData.fCompanyname = this.formObj[1].options[0].label;
                            this.formData.fCompanyid = this.formObj[1].options[0].value;
                        }

                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 中心实验室调标段
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                headers: {
                    "Content-Type": "application/json",
                    Authorization: localStorage.getItem("access_token"),
                },
            }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;
                    this.formObj[1].options = [];
                    data.map((item) => {
                        this.formObj[1].options.push({
                            label: item.sectionName,
                            value: item.sectionId,
                        });
                    });
                    this.formData.fCompanyname = this.formObj[1].options[0].label;
                    this.formData.fCompanyid = this.formObj[1].options[0].value;

                }
            });
        },
        getWBS(data) { //获取选择WBS信息
            this.formData.fProjecttypename = data.fLongname //工程部位
            this.formData.fProjecttypeid = data.fNodeid //工程部位id
        },

        closeDialog() {
            this.$emit("close");
        },


        // 数据字典
        getBaseDataitemDetailByItemCode(status) {
            getBaseDataitemDetailByItemCode({
                fItemcode: status
            }).then(res => {
                this.formObj[9].options = [];
                var data = res.data.baseDataitemdetailList
                var arr = []
                data.map(item => {
                    arr.push({
                        value: item.fItemdetailid,
                        label: item.fItemvalue
                    })
                })
                this.formObj[9].options = arr
            })
        },

        // 试验检测依据-获取试验依据，检测依据
        getCheckitemsList(num) {
            getCheckitemsList({
                modelType: num,
            })
                .then((res) => {
                    if (res.errcode == 0) {

                        if (num == 3) { //回弹试验依据
                            this.formObj[5].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[5].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId

                                })
                            })

                        } else if (num == 4) { //回弹判定依据
                            this.formObj[6].options = [];
                            res.data.checkitemsList.map(item => {
                                this.formObj[6].options.push({
                                    label: item.fItemno,
                                    value: item.fItemname,
                                    fId: item.fId
                                })
                            })

                        }

                    } else {
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 主要仪器设备名称及编号
        getEquipment() {
            getMangEquipmentPageList({
                // fUserid: this.userInfo.userId,
                fCompanyid: this.primaryInfo.fCompanyid, //当前登录用户机构id
                keyWord: "", //设备名称模糊查询字段
                keyType: "混凝土回弹检测仪器设备", //设备类型
                fNote: "1",
                modelType: "2", // 0代办 1已办
                sublevel: 0, //查看下级  1是0否
            })
                .then((res) => {
                    if (res.errcode == 0) {
                        this.formObj[7].options = [];
                        res.data.mangEquipmentList.list.map(item => {
                            this.formObj[7].options.push({
                                label: item.fEqptname + "/" + item.fEqptno,
                                value: item.fId
                            })
                        })
                    } else {
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 详情
        getDetail(row) {
            this.loading = true;
            getConcreteCheckTaskByFid({
                fId: row.fId
            }).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    this.formData = res.data.lrCheckConcreteTask;
                    if (this.statusNum != 7) {
                        if (row.fStatus == '被撤回') {
                            this.formObj[3].disabled = true;
                        } else {
                            this.formObj[3].disabled = false;
                        }
                    } else {
                        this.formObj[3].disabled = true;
                    }
                    //    检测依据
                    if (res.data.lrCheckConcreteTask.fTestnote != '') {
                        var fTestnote = []
                        this.formObj[5].options.map(item => {
                            res.data.lrCheckConcreteTask.fTestnote.split(",").map(i => {
                                if (item.label == i) {
                                    fTestnote.push(item)
                                }
                            })
                        })

                    } else {
                        fTestnote = []
                    }
                    //    检测依据
                    // 判定依据
                    if (res.data.lrCheckConcreteTask.fJudgetype != '') {
                        var fJudgetype = []
                        this.formObj[6].options.map(item => {
                            res.data.lrCheckConcreteTask.fJudgetype.split(",").map(i => {
                                if (item.label == i) {
                                    fJudgetype.push(item)
                                }
                            })
                        })

                    } else {
                        fJudgetype = []
                    }
                    // 判定依据
                    //    设备
                    if (res.data.lrCheckConcreteTask.fEquipmentid != '') {
                        var id = res.data.lrCheckConcreteTask.fEquipmentid.split(",")
                        var name = res.data.lrCheckConcreteTask.fEquipmentname.split(",")
                        var fEquipmentname = []
                        id.map((item, index) => {
                            fEquipmentname.push({
                                label: name[index],
                                value: item
                            })
                        })
                    } else {
                        fEquipmentname = []
                    }

                    //    设备
                    this.selected = [{
                        name: "fTestnote1",
                        selected: fTestnote,
                    },
                    {
                        name: "fJudgetype1",
                        selected: fJudgetype,
                    },
                    {
                        name: "fEquipmentname1",
                        selected: fEquipmentname,
                    },
                    ];

                } else {
                    this.loading = false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })

                }
            });
        },
        selectChange(data, a, b) {

            if (b == 'fTestnote1') {
                var arr = []
                data.map(item => {
                    if (item.label) {
                        arr.push(item.label)
                    } else {
                        arr.push(item)
                    }
                })
                this.formData.fTestnote = arr.join(",");
            } else if (b == 'fJudgetype1') {
                var arr1 = []
                data.map(item => {
                    if (item.label) {
                        arr1.push(item.label)
                    } else {
                        arr1.push(item)
                    }

                })
                this.formData.fJudgetype = arr1.join(",")
            } else if (b == 'fDirection') {

                if (data == '水平方向') {
                    this.formData.fSurface = "侧面";
                    this.formData.fAngle = "0°"
                    this.formObj[12].options = [{
                        label: "侧面",
                        value: "侧面"
                    }]
                    this.formObj[13].options = [{
                        label: "0°",
                        value: "0°"
                    }]
                } else if (data == '非水平方向') {
                    this.formData.fSurface = "表面";
                    this.formData.fAngle = "90°"
                    this.formObj[12].options = [{
                        label: "表面",
                        value: "表面"
                    }, {
                        label: "底面",
                        value: "底面"
                    }]
                    this.formObj[13].options = this.angleData
                }
            } else if (b == 'fCompanyname') {
                this.formData.fCompanyname = data.label;
                this.formData.fCompanyid = data.value;
                this.formData.fProjecttypename = ""
                this.formData.fProjecttypeid = ""
            }

            console.log("selectChange", data, a, b);
        },
        inputFocus(index, prop) {
            console.log("inputFocus", index, prop);
            if (prop == "fWitnessno") {
                this.isShow = true;
            }
            return index, prop;
        },


        yj_saveForm() {
            this.formData.userId = this.userInfo.id; // 当前登录人id
            this.formData.userName = this.userInfo.userName; // 当前登录人姓名
            // 设备
            var equipmentname = [];
            var equipmentnameid = [];
            this.formData.fEquipmentname1.map(item => {
                equipmentname.push(item.label);
                equipmentnameid.push(item.value)
            })
            this.formData.fEquipmentid = equipmentnameid.join(",")
            this.formData.fEquipmentname = equipmentname.join(",")
            // 设备

            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                console.log("保存", this.formData);
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }
        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            saveConcreteCheckTask(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$router.push({
                            path: "/reboundStrengthManage/reboundDetectionTask",
                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");

                        this.isLock = false
                        this.$message.success({
                            content: '新增成功',
                            duration: 1000
                        })
                    } else {
                        this.loading = false;
                        this.isLock = false;
                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },
        // 获取委托单列表数据
        closeDialog1() {
            this.isShow = false;
        },
        getEntrustRow(data) {
            // console.log("获取委托单列表数据", data);
            this.formData.fCompanyid = data.fCompanyid;//施工单位id
            this.formData.fCompanyname = data.fCompanyname;//施工单位名称
            this.formData.fProjectname = data.fProjectuse;//工程名称
            this.formData.fProjecttypeid = data.fProjecttypeid;//工程部位id
            this.formData.fProjecttypename = data.fProjecttypename;//工程部位
            this.formData.fSpecimenage = data.fSpecimenage;//龄期
            this.formData.fConcretelevel = data.fConcretelevel;//设计强度等级(MPa)
            this.formData.fConcretetype = data.fConcretetype;//是否泵送
            this.formData.fWitnessno = data.fEntrustno;//委托单编号
            this.formData.fWitnessid = data.fId;//委托单id
            this.formData.fTestnote = data.fTestnote;//检测依据
            this.formData.fJudgetype = data.fJudgetype;//判定依据
            // 检测依据
            if (data.fTestnote != '') {
                var fTestnote = []
                this.formObj[6].options.map(item => {
                    data.fTestnote.split(",").map(i => {
                        if (item.label == i) {
                            fTestnote.push(item)
                        }
                    })
                })

            } else {
                fTestnote = []
            }
            // 检测依据
            // 判定依据
            if (data.fJudgetype != '') {
                var fJudgetype = []
                this.formObj[7].options.map(item => {
                    data.fJudgetype.split(",").map(i => {
                        if (item.label == i) {
                            fJudgetype.push(item)
                        }
                    })
                })

            } else {
                fJudgetype = []
            }
            // 判定依据

            this.selected = [
                {
                    name: "fTestnote1",
                    selected: fTestnote,
                },
                {
                    name: "fJudgetype1",
                    selected: fJudgetype,
                }
            ];
        },
        // 获取委托单列表数据
    },
};